import {
	setStorageSync,
	getStorageSync
} from '@/utils/storage.js'
import {
	login,
	register,
	logout,
	retrievePassword,
	resetPassword,
	refreshToken
} from '@/api/user'
const globalModules = {
	state: {
		mainTabbar: [{
				iconPath: "/static/image/shelf.png",
				selectedIconPath: "/static/image/shelf_active.png",
				text: '书架',
				pagePath: '/pages/shelf/index'
			},
			{
				iconPath: "/static/image/home.png",
				selectedIconPath: "/static/image/home_active.png",
				text: '首页',
				pagePath: '/pages/home/index'
			},
			{
				iconPath: "/static/image/user.png",
				selectedIconPath: "/static/image/user_active.png",
				text: '我的',
				pagePath: '/pages/center/index'
			}
		],
		iStatusBarHeight: uni.getSystemInfoSync().statusBarHeight,
		screenHeight: uni.getSystemInfoSync().screenHeight,
		windowHeight: uni.getSystemInfoSync().windowHeight,
		token: getStorageSync('token') || '',
		name: getStorageSync('name') || '',
		email: getStorageSync('email') || '',
		loginInfo: getStorageSync('loginInfo') || {
			email: '',
			password: ''
		},
	},
	getters: {},
	mutations: {
		SET_TOKEN(state, token) {
			state.token = token
		},
		SET_NAME(state, name) {
			state.name = name
		},
		SET_EMAIL(state, email) {
			state.email = email
		},
		SET_USER_INFO(state, {
			token,
			name,
			email
		}) {
			state.token = token
			state.name = name
			state.email = email
			setStorageSync('token', token)
			setStorageSync('name', name)
			setStorageSync('email', email)
		},
		SET_LOGIN_INFO(state, {
			email,
			password
		}) {
			state.loginInfo = {
				email,
				password
			}
			setStorageSync('loginInfo', {
				email,
				password
			})
		}
	},
	actions: {
		// 刷新token
		refreshToken({commit}){
			return new Promise((resolve, reject) =>{
				refreshToken().then(res =>{
					commit('SET_TOKEN', res.data.token)
					setStorageSync('token', res.data.token)
					resolve(res.data.token)
				}).catch(e =>{
					reject(e)
				})
			})
		},
		// 重置token
		resetToken({
			commit
		}) {
			return new Promise((resolve, reject) => {
				commit('SET_TOKEN', '')
				commit('SET_NAME', '')
				setStorageSync('token', '')
				setStorageSync('name', '')
				resolve('ok')
			})
		},
		// 登陆
		login({
			commit
		}, {
			email,
			password
		}) {
			return new Promise((resolve, reject) => {
				login({
					email,
					password
				}).then(res => {
					commit('SET_USER_INFO', res.data)
					commit('SET_LOGIN_INFO', {
						email,
						password
					})
					resolve('ok')
				}).catch(e => {
					reject(e)
				})
			})
		},
		// 注册
		register({
			commit
		}, {
			name,
			email,
			password,
			password_confirmation,
			code
		}) {
			return new Promise((resolve, reject) => {
				register({
					name,
					email,
					password,
					password_confirmation,
					code
				}).then(res => {
					commit('SET_USER_INFO', res.data)
					resolve('ok')
				}).catch(e => {
					reject(e)
				})
			})
		},
		// 注销
		logout({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				let header = {
					'Authorization': 'Bearer ' + state.token
				}
				logout(header).then(res => {
					commit('SET_USER_INFO', res.data)
					commit('SET_BOOK_SHELF_LIST', [], {
						root: true
					})
					resolve('ok')
				}).catch(e => {
					reject(e)
				})
			})
		},
		// 找回密码
		retrievePassword({
			commit,
			state
		}, {
			email,
			password,
			password_confirmation
		}) {
			return new Promise((resolve, reject) => {
				retrievePassword({
					email,
					password,
					password_confirmation,
					token: state.token
				}).then(res => {
					commit('SET_USER_INFO', res.data)
					resolve('ok')
				}).catch(e => {
					reject(e)
				})
			})
		},
		// 修改密码
		resetPassword({
			commit,
			state
		}, {
			oldPassword,
			password,
			password_confirmation
		}) {
			return new Promise((resolve, reject) => {
				resetPassword({
					'Authorization': 'Bearer ' + state.token
				}, {
					oldPassword,
					password,
					password_confirmation
				}).then(res => {
					commit('SET_USER_INFO', res.data)
					resolve('ok')
				}).catch(e => {
					reject(e)
				})
			})
		}
	}
}

export default globalModules
